﻿namespace TfsCloudDeploy.Workflow.Core.Activities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Permissions;
    using Microsoft.Win32.SafeHandles;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Reflected from: Microsoft.TeamFoundation.SafeUnmanagedMemoryHandle.
    /// Assembly: Microsoft.TeamFoundation.Common, Version=10.0.0.0.
    /// </summary>
    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    internal sealed class SafeUnmanagedMemoryHandle : SafeHandleZeroOrMinusOneIsInvalid
    {
        // Methods
        internal SafeUnmanagedMemoryHandle()
            : base(true)
        {
        }

        internal SafeUnmanagedMemoryHandle(IntPtr preexistingHandle, bool ownsHandle)
            : base(ownsHandle)
        {
            base.SetHandle(preexistingHandle);
        }

        protected override bool ReleaseHandle()
        {
            if (base.handle != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(base.handle);
                base.handle = IntPtr.Zero;
                return true;
            }
            return false;
        }
    }
}
